var box2d = {
   b2Vec2 : Box2D.Common.Math.b2Vec2,
   b2BodyDef : Box2D.Dynamics.b2BodyDef,
   b2Body : Box2D.Dynamics.b2Body,
   b2FixtureDef : Box2D.Dynamics.b2FixtureDef,
   b2Fixture : Box2D.Dynamics.b2Fixture,
   b2World : Box2D.Dynamics.b2World,
   b2MassData : Box2D.Collision.Shapes.b2MassData,
   b2PolygonShape : Box2D.Collision.Shapes.b2PolygonShape,
   b2CircleShape : Box2D.Collision.Shapes.b2CircleShape,
   b2DebugDraw : Box2D.Dynamics.b2DebugDraw
};

var SCALE = 30;
var stage, world, player, canvas, debug_canvas;

function init() {
	canvas = document.getElementById('canvas');
	debug_canvas = document.getElementById('debug_canvas');
	
	stage = new createjs.Stage(canvas);
	//debug_stage = new createjs.Stage(debug_canvas);
	
	setupPhysics();
	setupImages();
	
	player = new Player();
	
	window.addEventListener('keydown', function(event) { Key.onKeyDown(event); }, false);
	window.addEventListener('keyup', function(event) { Key.onKeyUp(event); }, false);
	
	//debug_canvas.addEventListener("mousedown", function(e) { alert('yay');}, false);
	
	createjs.Ticker.addListener(window);
	createjs.Ticker.setFPS(60);
	createjs.Ticker.useRAF = true;
}

function setupPhysics() {
	world = new box2d.b2World(new box2d.b2Vec2(0,50), true);
	
	
	
	
	// setup debug draw (box2d)
	var debugDraw = new box2d.b2DebugDraw();
	debugDraw.SetSprite(debug_canvas.getContext('2d'));
	debugDraw.SetFillAlpha(0.4);
	debugDraw.SetDrawScale(SCALE);
	debugDraw.SetFlags(box2d.b2DebugDraw.e_shapeBit | box2d.b2DebugDraw.e_jointBit);
	world.SetDebugDraw(debugDraw);
	
	
	//Add listeners for contact
	var listener = new Box2D.Dynamics.b2ContactListener;

	listener.BeginContact = function(contact) {
	    //console.log(contact.GetFixtureA().GetBody().GetUserData());
	}

	listener.EndContact = function(contact) {
	    // console.log(contact.GetFixtureA().GetBody().GetUserData());
	}

	listener.PostSolve = function(contact, impulse) {
		
	    if (contact.GetFixtureA().GetBody() == player.playerBody/* || contact.GetFixtureB().GetBody() == player.playerBody*/) {
		var worldManifold = new Box2D.Collision.b2WorldManifold
		contact.GetWorldManifold(worldManifold)
		    //console.log(worldManifold)
		if(worldManifold.m_normal.y > 0) {
			var vec = player.playerBody.GetLinearVelocity()
			if(player.jumping > 1) { // <--- without this we can do a double jump :D
				//vec.y = 0
				//SetLinearVelocity(vec)
				//console.log(vec)
				player.jumping = false;
			} else if(player.jumping == 1){
				player.jumping = 2;
			}
		}
		
	    }
	    console.log(player.playerBody)
	}
	
	listener.PreSolve = function(contact, oldManifold) {
	    // PreSolve
	}

	world.SetContactListener(listener);
}

function setupImages() {
}

function tick() {
	stage.update();
	
	handleInput();
	player.update();
	
	world.DrawDebugData();
	world.Step(1/60, 10, 10);
	world.ClearForces();
	
}

function handleInput() {
	if(Key.isDown(Key.UP)) player.moveUp();
	if(Key.isDown(Key.LEFT)) player.moveLeft();
	if(Key.isDown(Key.DOWN)) player.moveDown();
	if(Key.isDown(Key.RIGHT)) player.moveRight();
	if(Key.isDown(Key.W)) player.shootUp();
	if(Key.isDown(Key.A)) player.shootLeft();
	if(Key.isDown(Key.S)) player.shootDown();
	if(Key.isDown(Key.D)) player.shootRight();
}

var Key = {
	_pressed: {},

	LEFT: 37,
	UP: 38,
	RIGHT: 39,
	DOWN: 40,
	W: 87,
	A: 65,
	S: 83, 
	D: 68,
	
	isDown: function(keyCode) {
		return this._pressed[keyCode];
	},
	
	onKeyDown: function(event) {
		this._pressed[event.keyCode] = true;
	},
	  
	onKeyUp: function(event) {
		delete this._pressed[event.keyCode];
	}
};